package com.asset.dispoal.dataDetail.service.commom;

import com.asset.dispoal.dataDetail.domain.db.mapper.SysDataDetailMapper;
import com.asset.dispoal.dataDetail.dto.SysDataDetailGeneratorRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;

import java.util.List;
import java.util.Map;

@Component
public class SysDataTypeGeneratorCommand implements BaseCommand<SysDataDetailGeneratorRequest, Integer> {

    @Autowired
    SysDataDetailMapper sysDataDetailMapper;

    @Autowired
    private BaseIdeable<String> idWorker;

    public Integer execute(SysDataDetailGeneratorRequest request) {
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        List<Map<String, Object>> maps = sysDataDetailMapper.queryTypeTemplateMapById(request.getId());
        maps.forEach(map -> {
            map.put("id", idWorker.generateId());
            map.put("createUser", userId);
            map.put("assetInstBatchId", request.getAssetInstBatchId());
            map.put("createTime", DateTimeUtil.currentDate());
        });
        sysDataDetailMapper.deleteByAssetInstBatchId(request.getAssetInstBatchId());
        sysDataDetailMapper.initialization(maps);
        return 1;
    }
}
